package top.lyc829.zzl_android.ui.team.team_detail;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.MyApp;
import top.lyc829.zzl_android.logic.bean.ActivityBean;
import top.lyc829.zzl_android.logic.bean.BaseBean;
import top.lyc829.zzl_android.logic.bean.BaseDetailBean;
import top.lyc829.zzl_android.logic.bean.MyTeamInfoBean;
import top.lyc829.zzl_android.logic.bean.MyTeamState;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Url;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

/**
 * @Author lyc
 * @create 2021-03-30 13:56
 */
public class TeamDetailViewModel extends ViewModel {

    private MutableLiveData<Integer> teamId;
    private MutableLiveData<MyTeamInfoBean> teamInfoLiveData;
    private MutableLiveData<List<ActivityBean>> activitiesLiveData;
    private MutableLiveData<Boolean> isJoinTeamLiveData;

    private MutableLiveData<List<BaseDetailBean>> baseBeanLiveData;

    private MutableLiveData<MyTeamState> myTeamStateLiveData;

    private MutableLiveData<Integer> isFellowLiveData;

    public MutableLiveData<MyTeamState> getMyTeamStateLiveData() {
        if (myTeamStateLiveData == null) {
            myTeamStateLiveData = new MutableLiveData<>();
            myTeamStateLiveData.setValue(new MyTeamState("-1", -1));
        }
        return myTeamStateLiveData;
    }



    public MutableLiveData<List<BaseDetailBean>> getBaseBeanLiveData() {
        if (baseBeanLiveData == null) {
            baseBeanLiveData = new MutableLiveData<>();
            if (baseBeanLiveData.getValue() == null) {
                baseBeanLiveData.setValue(new ArrayList<>());
            }
        }
        return baseBeanLiveData;
    }

    public MutableLiveData<Boolean> getIsJoinTeamLiveData() {
        if (isJoinTeamLiveData == null) {
            isJoinTeamLiveData = new MutableLiveData<>();
        }
        return isJoinTeamLiveData;
    }

    public MutableLiveData<Integer> getTeamId() {
        if (teamId == null) {
            teamId = new MutableLiveData<>();
        }
        return teamId;
    }

    public MutableLiveData<MyTeamInfoBean> getTeamInfoLiveData() {
        if (teamInfoLiveData == null) {
            teamInfoLiveData = new MutableLiveData<>();
        }
        return teamInfoLiveData;
    }

    public MutableLiveData<List<ActivityBean>> getActivitiesLiveData() {
        if (activitiesLiveData == null) {
            activitiesLiveData = new MutableLiveData<>();
        }
        return activitiesLiveData;
    }

    public MutableLiveData<Integer> getIsFellowLiveData() {
        if (isFellowLiveData == null) {
            isFellowLiveData = new MutableLiveData<>();
        }
        return isFellowLiveData;
    }

    public void getTeamInfo(int teamId) {


        Network.getTeamInfo(teamId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {

                String resStr = res.body().string();

                ResultBean<MyTeamInfoBean> resultBean = JsonUtilNew
                        .fromJsonObject(resStr, MyTeamInfoBean.class);

                if (resultBean.getCode().equals(200)) {
                    getTeamInfoLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void getTeamActivities(int teamId) {
        Network.getActivityInTeam(teamId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                ResultBean<List<ActivityBean>> resultBean = JsonUtilNew
                        .fromJsonArray(res.body().string(), ActivityBean.class);

                if (resultBean.getCode().equals(200)) {
                    getActivitiesLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void getIsJoinTeam(int userId, int teamId) {
        Network.isJoinTeam(userId, teamId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                ResultBean<Boolean> resultBean = JsonUtilNew
                        .fromJsonObject(res.body().string(), Boolean.class);

                if (resultBean.getCode().equals(200)) {
                   getIsJoinTeamLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public synchronized void getBasesInfo(int baseId) {
        HashMap<String, String> body = new HashMap<>();
        body.put("id", String.valueOf(baseId));
        HttpUtil httpUtil = HttpUtil.getInstance(MyApp.getContext());
        httpUtil.get(Url.GET_BASE_DETAIL, body, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                try {
                    String resultStr = res.body().string();
                    ResultBean<BaseDetailBean> resultBean = JsonUtilNew.fromJsonObject(resultStr, BaseDetailBean.class);
                    if (resultBean.getCode().equals(200)) {
                        List<BaseDetailBean> list = getBaseBeanLiveData().getValue();

                        list.add(resultBean.getData());
                        getBaseBeanLiveData().postValue(list);
                        Utils.Sout("基地个数：" + list.size());
                    } else {
                        failed(new IOException(resultBean.getMsg()));
                    }
                } catch (Exception e) {
                    failed(new IOException(e.getMessage()));
                }

            }

            @Override
            public void failed(IOException e) {
                Utils.SoutErr(e.getMessage());
            }
        });
    }


    // 获取用户参加的团队状态
    public void getMyTeamIdFromServer(int userId) {
        Network.getIsInTeam(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<MyTeamState> resultBean = JsonUtilNew.fromJsonObject(resStr, MyTeamState.class);
                // 无团队
//                if (resultBean.getCode().equals(361)) {
//                    getMyTeamStateLiveData().postValue(new MyTeamState("-1", -1));
//                }
                if (resultBean.getCode().equals(200)) {
                    getMyTeamStateLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    /**
     * 获取关注关系
     * @param userId 用户id
     * @param type 类型
     * @param typeId 目的id
     */
    public void getFellowRelationShip(int userId, int type, int typeId) {
        Network.getAttentionRelationship(userId, type, typeId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resultStr = res.body().string();
                TypeToken<ResultBean<Integer>> typeToken = new TypeToken<ResultBean<Integer>>() {};
                ResultBean<Integer> resultBean = new Gson().fromJson(resultStr, typeToken.getType());

                if (resultBean.getCode().equals(200)) {
                    getIsFellowLiveData().postValue(resultBean.getData());
                }else if (resultBean.getCode().equals(201)) {
                    getIsFellowLiveData().postValue(0);
                }  else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                getIsFellowLiveData().postValue(0);
                Utils.Sout(e.getMessage());
            }
        });
    }

}
